package com.lqm.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lqm.common.utils.PageUtils;
import com.lqm.common.utils.Query;
import com.lqm.common.utils.R;
import com.lqm.mall.ware.dao.WareSkuDao;
import com.lqm.mall.ware.entity.PurchaseDetailEntity;
import com.lqm.mall.ware.entity.WareSkuEntity;
import com.lqm.mall.ware.feign.ProductFeignService;
import com.lqm.mall.ware.service.WareSkuService;
import com.lqm.mall.ware.vo.SkuHasStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    WareSkuDao wareSkuDao;
    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wrapper=new QueryWrapper<WareSkuEntity>();
        //skuId=&wareId=
        String skuId=(String) params.get("skuId");
        if(!StringUtils.isEmpty(skuId) && !skuId.equals("0")){
            wrapper.eq("sku_id",skuId);
        }
        String wareId=(String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId) && !wareId.equals("0")){
            wrapper.eq("ware_id",wareId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(PurchaseDetailEntity entity) {
        //先判断有没有相关记录
        List<WareSkuEntity> entities=wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id",entity.getSkuId()).eq("ware_id",entity.getWareId()));
        if(entities==null || entities.size()==0){
            //没有插入
            WareSkuEntity wareSkuEntity=new WareSkuEntity();
            wareSkuEntity.setSkuId(entity.getSkuId());
            wareSkuEntity.setWareId(entity.getWareId());
            wareSkuEntity.setStock(entity.getSkuNum());
            wareSkuEntity.setStockLocked(0);
            //远程查询sku名字,失败无需回滚
            //TODO 其他不回滚事务的方法
            try{
                R r=productFeignService.info(entity.getSkuId());
                Map<String,Object> info = (Map<String, Object>) r.get("skuInfo");
                if(r.getCode()==0){
                    wareSkuEntity.setSkuName((String) info.get("skuName"));
                }
            }catch (Exception e){

            }
            wareSkuDao.insert(wareSkuEntity);
        }else{
            wareSkuDao.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
        }
    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> collect=skuIds.stream().map((skuId)->{
            SkuHasStockVo vo=new SkuHasStockVo();
            Long stock=this.baseMapper.getStock(skuId);
            vo.setSkuId(skuId);
            vo.setHasStock((stock!=null?stock>0:false));
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }


}